package admin.service.impl;

import admin.entity.User;
import admin.mapper.RoleMapper;
import admin.mapper.UserMapper;
import admin.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final RoleMapper roleMapper;

    @Override
    public void page( Page<User> page,User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class)
                .eq(!ObjectUtils.isEmpty(user.getId()), User::getId, user.getId())
                .eq(!ObjectUtils.isEmpty(user.getUsername()), User::getUsername, user.getUsername())
                .like(!ObjectUtils.isEmpty(user.getName()), User::getName, user.getName());
        this.page(page,wrapper);
        for (User u : page.getRecords()) {
            u.setRole(roleMapper.selectById(u.getRoleId()));
        }
    }
}
